package com.jpatest.demo.controller;

import com.jpatest.demo.Service.CompanyServiceImpl;
import com.jpatest.demo.entity.Company;
import com.jpatest.demo.entity.PageEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "company")
public class CompanyController {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    CompanyServiceImpl companyService;

    @RequestMapping(value = "addCompany", method = RequestMethod.POST)
    public @ResponseBody
    Object addCompany(@RequestBody Company company){
        Company rescompany = companyService.addCompany(company);
        return rescompany;
    }

    @RequestMapping(value = "findAllCompany", method = RequestMethod.POST)
    public @ResponseBody Object findAllCompany(@RequestBody PageEntity pageEntity){
        List<Company> list = companyService.findAllCompany(pageEntity.getPageNum(),pageEntity.getPageSize());
        pageEntity.setData(list);
        return pageEntity;
    }

    @RequestMapping(value = "deleteCompany", method = RequestMethod.POST)
    public @ResponseBody Object deleteCompany(@RequestBody int id){
        int result = companyService.deleteCompany(id);
        return result;
    }

    @RequestMapping(value = "getCompany", method = RequestMethod.POST)
    public @ResponseBody Company getCompany(@RequestBody int id){
        Company company = companyService.getCompanyById(id);
        return company;
    }

    @RequestMapping(value = "updateCompany", method = RequestMethod.POST)
    public @ResponseBody Company updateCompany(@RequestBody Company company){
        Company resCompany = companyService.updateCompany(company);
        return resCompany;
    }
}
